package com.genericdevice.platform.pages.plugins.mvp;

import com.genericdevice.platform.core.define.EventConstants;
import com.genericdevice.platform.core.dialog.mvp.FileDialogPresenter;
import com.genericdevice.platform.core.event.IEvent;
import com.genericdevice.platform.core.event.listeners.DefaultListener;
import com.genericdevice.platform.core.event.EventBus;
import com.genericdevice.platform.core.mvp.AbstractPresenter;
import com.genericdevice.platform.core.plugins.PluginsLoader;

/**
 * Created by Xilent on 18.05.2014.
 */
public class PluginsPresenter extends AbstractPresenter<PluginsView> {
    @Override
    public void init() {
        super.init();

        EventBus.addListener(EventConstants.PLUGIN_ADDED, new DefaultListener(false) {
            @Override
            public void onSuccess(IEvent event) {
                PluginsLoader.addPlugin((String) event.getEventParam(FileDialogPresenter.FILE_PATH));
                view.refresh();
            }
        });

        EventBus.addListener(EventConstants.PLUGIN_CLEAR_PRESSED, new DefaultListener(false) {
            @Override
            public void onSuccess(IEvent event) {
                PluginsLoader.clearPluginsList();
                view.refresh();
            }
        });
    }

    @Override
    protected PluginsView createView() {
        return new PluginsView();
    }

    @Override
    public void destory() {
        EventBus.removeEventListeners(EventConstants.PLUGIN_ADD_PRESSED);

        super.destory();
    }

    public void onPluginSelected(Integer index) {
        PluginsLoader.selectPlugin(index);
        EventBus.fireEvent(EventConstants.PLUGIN_CHANGED);
        activity.finish();
    }
}
